/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GraphicalSolverFrame.java
 *
 * Created on Jun 22, 2010, 3:11:51 PM
 */
package GraphicalSolverTSP;

import AntColonyOptimization_TSP.*;

import java.util.ArrayList;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;





/**
 *
 * @author bogdan
 */
public class GraphicalSolverFrame extends javax.swing.JFrame {
    private static final int STATE_NEW = 0;
    private static final int STATE_INIT = 1;
    private static final int STATE_RUNNING = 2;

    private static int currentState;

    private TSPSolver solver;

    
    /** Creates new form GraphicalSolverFrame */
    public GraphicalSolverFrame() {
        initComponents();

        currentState = STATE_NEW;
        jButtonLoad.setEnabled(true);
        jButtonSave.setEnabled(true);
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        jButton5.setEnabled(false);
        jButton6.setEnabled(true);

        jComboBox1.setEnabled(true);
        
    	solver = new TSPSolver();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
    	

        jPanel1 = new javax.swing.JPanel();
        canvas1 = new java.awt.Canvas();
        jPanel2 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        jSpinner1 = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jSpinner2 = new javax.swing.JSpinner();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jButtonLoad = new javax.swing.JButton();
        jButtonSave = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Graphical Traveling Salesman Problem Solver");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Input"));

        canvas1.setBackground(new java.awt.Color(255, 255, 255));
        canvas1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                canvas1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, 680, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(canvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Options", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ant System", "Ant Colony System", "MaxMin Ant System", "Genetic Algorithm", "Genetic Ant System" }));

        jLabel1.setText("Method:");

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Show Best So Far Tour");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Ant Colony options:");

        jSpinner1.setValue(10);

        jLabel3.setText("Number of Ants:");

        jTextField1.setText("1.0");

        jLabel4.setText("Alfa:");

        jTextField2.setText("3.0");

        jLabel5.setText("Beta:");

        jTextField3.setText("0.5");

        jLabel6.setText("Global evap. Rate:");

        jTextField4.setText("0.1");

        jLabel7.setText("Local evap. Rate:");

        jTextField5.setText("0.7");

        jLabel8.setText("Pseudo-random coeficient:");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel9.setText("Genetic Algorithm options:");

        jSpinner2.setValue(10);

        jLabel10.setText("Population Size:");

        jLabel11.setText("Mutation Ratio:");

        jTextField6.setText("0.1");

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel12.setText("Optimization options:");

        jCheckBox2.setText("2-opt");

        jCheckBox3.setText("3-opt");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBox1, 0, 123, Short.MAX_VALUE))
                    .addComponent(jCheckBox1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel9)
                    .addComponent(jLabel12)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jCheckBox2)
                        .addGap(18, 18, 18)
                        .addComponent(jCheckBox3))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11))
                        .addGap(4, 4, 4)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField6, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                            .addComponent(jSpinner2, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox2)
                    .addComponent(jCheckBox3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Controls", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jButtonLoad.setText("Load");
        jButtonLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoadActionPerformed(evt);
            }
        });
        
        jButtonSave.setText("Save");
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });
        
        jButton1.setText("New");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Init");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Step");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Start");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Stop");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Reset");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonLoad, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonSave, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jButtonLoad)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void canvas1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvas1MouseClicked
        // Adaugarea unui oras in graf
        if(currentState == STATE_NEW){
            int coordX = evt.getX();
            int coordY = evt.getY();
            solver.points.add(new PointCoords(coordX, coordY));
            drawCanvas();
        }
        else if ((currentState == STATE_INIT)||(currentState == STATE_RUNNING)){
            if(((String)jComboBox1.getSelectedItem()).equals("Ant System")){

            }
        }
    }//GEN-LAST:event_canvas1MouseClicked

    private void jButtonLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoadActionPerformed
        // Actiunile butonului NEW
        currentState = STATE_NEW;
        // Activarea dezactivarea componentelor pentru starea NEW
        jButtonLoad.setEnabled(true);
        jButtonSave.setEnabled(true);
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        jButton5.setEnabled(false);
        jButton6.setEnabled(true);
        
        jTextField1.setEnabled(true);
        jTextField2.setEnabled(true);
        jTextField3.setEnabled(true);
        jTextField4.setEnabled(true);
        jTextField5.setEnabled(true);
        jTextField6.setEnabled(true);

        jSpinner1.setEnabled(true);
        jSpinner2.setEnabled(true);

        jCheckBox2.setEnabled(true);
        jCheckBox3.setEnabled(true);

        jComboBox1.setEnabled(true);

        solver.loadFromFile();
        drawCanvas();

        

    }//GEN-LAST:event_jButton1ActionPerformed


    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        // Actiunile butonului NEW
        currentState = STATE_NEW;
        // Activarea dezactivarea componentelor pentru starea NEW
        jButtonLoad.setEnabled(true);
        jButtonSave.setEnabled(true);
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        jButton5.setEnabled(false);
        jButton6.setEnabled(true);
        
        jTextField1.setEnabled(true);
        jTextField2.setEnabled(true);
        jTextField3.setEnabled(true);
        jTextField4.setEnabled(true);
        jTextField5.setEnabled(true);
        jTextField6.setEnabled(true);

        jSpinner1.setEnabled(true);
        jSpinner2.setEnabled(true);

        jCheckBox2.setEnabled(true);
        jCheckBox3.setEnabled(true);

        jComboBox1.setEnabled(true);

        solver.saveToFile();

        

    }//GEN-LAST:event_jButton1ActionPerformed


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Actiunile butonului NEW
        currentState = STATE_NEW;
        // Activarea dezactivarea componentelor pentru starea NEW
        jButtonLoad.setEnabled(true);
        jButtonSave.setEnabled(true);
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        jButton5.setEnabled(false);
        jButton6.setEnabled(true);
        
        jTextField1.setEnabled(true);
        jTextField2.setEnabled(true);
        jTextField3.setEnabled(true);
        jTextField4.setEnabled(true);
        jTextField5.setEnabled(true);
        jTextField6.setEnabled(true);

        jSpinner1.setEnabled(true);
        jSpinner2.setEnabled(true);

        jCheckBox2.setEnabled(true);
        jCheckBox3.setEnabled(true);

        jComboBox1.setEnabled(true);

        solver.points.clear();
        drawCanvas();

        

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // Actiunile butonului INIT
        currentState = STATE_INIT;
        // Activarea dezactivarea componentelor pentru starea INIT
        jButtonLoad.setEnabled(true);
        jButtonSave.setEnabled(true);
        jButton1.setEnabled(true);
        jButton2.setEnabled(false);
        jButton3.setEnabled(true);
        jButton4.setEnabled(true);
        jButton5.setEnabled(false);
        jButton6.setEnabled(true);

        
        jTextField1.setEnabled(false);
        jTextField2.setEnabled(false);
        jTextField3.setEnabled(false);
        jTextField4.setEnabled(false);
        jTextField5.setEnabled(false);
        jTextField6.setEnabled(false);

        jSpinner1.setEnabled(false);
        jSpinner2.setEnabled(false);

        jCheckBox2.setEnabled(false);
        jCheckBox3.setEnabled(false);

        jComboBox1.setEnabled(false);

        // retragerea datelor din formulare
        solver.noAnts = ((Integer)jSpinner1.getValue()).intValue();
        solver.popSize = ((Integer)jSpinner2.getValue()).intValue();
        solver.noNodes = solver.points.size();
        try{
        	solver.alfa = Double.parseDouble(jTextField1.getText());
        	solver.beta = Double.parseDouble(jTextField2.getText());
        	solver.globalEvapRate = Double.parseDouble(jTextField3.getText());
        	solver.localEvapRate = Double.parseDouble(jTextField4.getText());
        	solver.pseudoRandCoef = Double.parseDouble(jTextField5.getText());
        	solver.mutationRatio = Double.parseDouble(jTextField6.getText());
        }
        catch(Exception e){
            System.err.println("Wrong params.");
        }
        solver.doOpt2 = jCheckBox2.isSelected();
        solver.doOpt3 = jCheckBox3.isSelected();
        
        solver.init();

        // desenare
        drawCanvas();


    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // Actiunile butonului STEP
        step();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // Actiunile butonului START
        currentState = STATE_RUNNING;
        // Activarea dezactivarea componentelor pentru starea RUNNING
        jButtonLoad.setEnabled(false);
        jButtonSave.setEnabled(false);
        jButton1.setEnabled(false);
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        jButton5.setEnabled(true);
        jButton6.setEnabled(false);
        jComboBox1.setEnabled(false);

        
        RunThread thread = new RunThread();
        thread.start();

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // Actiunile butonului PAUSE
        currentState = STATE_INIT;
        // Activarea dezactivarea componentelor pentru starea INIT
        jButtonLoad.setEnabled(true);
        jButtonSave.setEnabled(true);
        jButton1.setEnabled(true);
        jButton2.setEnabled(false);
        jButton3.setEnabled(true);
        jButton4.setEnabled(true);
        jButton5.setEnabled(false);
        jButton6.setEnabled(true);

        jComboBox1.setEnabled(false);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // Actiunile pentru butonul RESET
        
        currentState = STATE_NEW;
        // Activarea dezactivarea componentelor pentru starea NEW
        jButtonLoad.setEnabled(true);
        jButtonSave.setEnabled(true);
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        jButton5.setEnabled(false);
        jButton6.setEnabled(true);

        jTextField1.setEnabled(true);
        jTextField2.setEnabled(true);
        jTextField3.setEnabled(true);
        jTextField4.setEnabled(true);
        jTextField5.setEnabled(true);
        jTextField6.setEnabled(true);

        jSpinner1.setEnabled(true);
        jSpinner2.setEnabled(true);

        jCheckBox2.setEnabled(true);
        jCheckBox3.setEnabled(true);

        jComboBox1.setEnabled(true);

        drawCanvas();
    }//GEN-LAST:event_jButton6ActionPerformed
    private void drawBestTour(int[] tour, Graphics2D gmem){
        float dash1[] = {10.0f, 3.0f, 5.0f};
        BasicStroke dashed = new BasicStroke(5.0f,
                                          BasicStroke.CAP_BUTT,
                                          BasicStroke.JOIN_MITER,
                                          10.0f, dash1, 0.0f);


        gmem.setStroke(dashed);
        gmem.setColor(Color.blue);
        for(int i = 0; i < tour.length-1; i++){
            gmem.drawLine(solver.points.get(tour[i]).getX(), solver.points.get(tour[i]).getY(),
            		solver.points.get(tour[i+1]).getX(), solver.points.get(tour[i+1]).getY());
        }
        
    }
    private void drawGraphPlain(Graphics2D gmem){
        for(PointCoords p: solver.points){
            gmem.fillOval(p.getX()-5, p.getY()-5, 10, 10);
        }


        for(int i = 0; i < solver.points.size()-1; i++){
            for(int j = i+1; j < solver.points.size(); j++){
                gmem.drawLine(solver.points.get(i).getX(), solver.points.get(i).getY(),solver.points.get(j).getX(), solver.points.get(j).getY());
            }
        }
    }
    private void drawGraphPheromone(Graphics2D gmem, double[][] ph){
        int noIntervals = 5;
        double maxPh = Double.NEGATIVE_INFINITY;
        double minPh = Double.POSITIVE_INFINITY;
        
        // Calculez valoarea maxima si minima de feromoni asociate unei muchii
        for(int i = 0; i < solver.noNodes; i++){
            for(int j = 0; j < solver.noNodes; j++){
                if(i!=j){
                    if(maxPh < ph[i][j])
                        maxPh = ph[i][j];
                    if(minPh > ph[i][j])
                        minPh = ph[i][j];
                }
            }
        }
        // Desenarea grafului folosind concentratia de feromoni
        double delta = (maxPh-minPh)/((double)noIntervals);
        
        for(int i = 0; i < solver.noNodes-1; i++)
            for(int j = i+1; j < solver.noNodes; j++){
                double phij = ph[i][j];
                if(delta>0.0){
                    int intv = (int)((phij-minPh)/delta);
                    gmem.setColor(new Color((intv)*(255/noIntervals),0,0));
                    gmem.setStroke(new BasicStroke(1.0f+(float)((float)intv/(float)noIntervals)*2.0f));
                }
                else{
                    gmem.setColor(Color.RED);
                }
                gmem.drawLine(solver.points.get(i).getX(),solver.points.get(i).getY(),solver.points.get(j).getX(),solver.points.get(j).getY());
            }
        gmem.setColor(Color.BLACK);
        //Desen puncte
        for(int i = 0; i < solver.noNodes; i++)
            gmem.fillOval(solver.points.get(i).getX()-5, solver.points.get(i).getY()-5, 10, 10);
    }
    private void drawCanvas(){
        int i,j;
        Graphics g = (Graphics2D)canvas1.getGraphics();
        Dimension d = canvas1.getSize();
        Image img = canvas1.createImage(d.width,d.height);
        Graphics2D gmem = (Graphics2D)img.getGraphics();

        //gmem.clearRect(0, 0, d.width, d.height);
        
        if(currentState == STATE_NEW){
            drawGraphPlain(gmem);
        }
        else if((currentState == STATE_INIT)||(currentState == STATE_RUNNING)){
            if(((String)jComboBox1.getSelectedItem()).equals("Ant System")){
                
                drawGraphPheromone(gmem, solver.as.getPheromoneMatrix());
                if(jCheckBox1.isSelected()){
                    drawBestTour(solver.as.getBestSoFarTour(), gmem);
                }
                gmem.setColor(Color.black);
                gmem.drawString("Iterations: "+solver.as.getIteration(), 10, 10);
                gmem.drawString("Tour Length: "+solver.as.computeTourLength(solver.as.getBestSoFarTour()), 10, 20);
            }
            if(((String)jComboBox1.getSelectedItem()).equals("MaxMin Ant System")){
                
                drawGraphPheromone(gmem, solver.mmas.getPheromoneMatrix());
                if(jCheckBox1.isSelected()){
                    drawBestTour(solver.mmas.getBestSoFarTour(), gmem);
                }
                gmem.setColor(Color.black);
                gmem.drawString("Iterations: "+solver.mmas.getIteration(), 10, 10);
                gmem.drawString("Tour Length: "+solver.mmas.computeTourLength(solver.mmas.getBestSoFarTour()), 10, 20);
            }
            if(((String)jComboBox1.getSelectedItem()).equals("Ant Colony System")){
                
                drawGraphPheromone(gmem, solver.acs.getPheromoneMatrix());
                if(jCheckBox1.isSelected()){
                    drawBestTour(solver.acs.getBestSoFarTour(), gmem);
                }
                gmem.setColor(Color.black);
                gmem.drawString("Iterations: "+solver.acs.getIteration(), 10, 10);
                gmem.drawString("Tour Length: "+solver.acs.computeTourLength(solver.acs.getBestSoFarTour()), 10, 20);
            }
            if(((String)jComboBox1.getSelectedItem()).equals("Genetic Algorithm")){
                
                drawGraphPlain(gmem);
                if(jCheckBox1.isSelected()){
                    drawBestTour(solver.ga.getBestSoFarTour(), gmem);
                }
                gmem.setColor(Color.black);
                gmem.drawString("Iterations: "+solver.ga.getIteration(), 10, 10);
                gmem.drawString("Tour Length: "+solver.ga.computeTourLength(solver.ga.getBestSoFarTour()), 10, 20);
            }
            if(((String)jComboBox1.getSelectedItem()).equals("Genetic Ant System")){
                
                drawGraphPheromone(gmem, solver.gas.getPheromoneMatrix());
                if(jCheckBox1.isSelected()){
                    drawBestTour(solver.gas.getBestSoFarTour(), gmem);
                }
                gmem.setColor(Color.black);
                gmem.drawString("Iterations: "+solver.gas.getIteration(), 10, 10);
                gmem.drawString("Tour Length: "+solver.gas.computeTourLength(solver.gas.getBestSoFarTour()), 10, 20);
            }
            gmem.drawString("Execution Time: "+(solver.executionTime/1000000)+" ms", 10, 30);
        }
        g.drawImage(img, 0, 0, canvas1);
        gmem.dispose();
    }

    private void step(){
    	solver.step((String)jComboBox1.getSelectedItem());
        drawCanvas();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GraphicalSolverFrame().setVisible(true);
            }
        });
    }
    class RunThread extends Thread{
        @Override
        public void run(){
            while(currentState == STATE_RUNNING){
                step();
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Canvas canvas1;
    private javax.swing.JButton jButtonLoad;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables

}
